require("../../assets/css/index.less")
require("../../assets/lib/swiper/swiper.css")
require('weui')
require("./home.less")

const weui = require("weui.js")
const Swiper = require("../../assets/lib/swiper/swiper.js")
const { getEle, publicFooter, isLogin, ready, addEvent, } = require("../../utils/dom")
const { get, post } = require('../../utils/https')

isLogin()


ready(() => {
    publicFooter()

    new Swiper('.swiper', {
        loop: true,
        pagination: {
            el: '.swiper-pagination',
        },
        autoplay: {
            delay: 3000,
            stopOnLastSlide: false,
            disableOnInteraction: false,
        }
    })

    function getUserInfo() {
        get("/api/user/info", res => {
            let { errno, data } = res
            let { ranking, clockCount, badges } = data
            if (errno == 0) {
                getEle("#ranking").innerHTML = ranking
                getEle("#clockCount").innerHTML = clockCount
                getEle("#badges").innerHTML = badges
            }
        })
    }
    getUserInfo()

    function getClockInfo() {
        get("/api/user/clockInInfo", res => {
            let { errno, data } = res
            console.log(data)
            if (errno == 0) {
                getEle("#clockCount").innerHTML = data.clockCount
                getEle("#clockBtn").innerHTML = data.isClockIn ? '已打卡' : '今日打卡'
                console.log(data.isClockIn)
            }
        })
    }
    getClockInfo()

    addEvent("#clockBtn", () => {
        post("/api/user/clockIn", {}, res => {
            let { errno, data } = res
            if (errno == 0) {
                weui.toast('恭喜您,打卡成功!!')
                getClockInfo()
            } else {
                weui.alert("今日已打卡")
            }
        })
    })

})

